package com.jlf.starter.openapi;

import io.swagger.v3.oas.models.ExternalDocumentation;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Contact;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.info.BuildProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * openapi的自动配置
 *
 * @author wujr
 * 2022/7/7
 * @version 1.0
 * 【变更历史】
 * [wujr 2022/7/7 1.0 新增]
 */
@Configuration
@RequiredArgsConstructor
public class SpringDocAutoConfiguration {//implements WebMvcConfigurer {
    private static final String SECURITY_SCHEME_NAME = "BearerAuth";
    @Value("${spring.swagger.title}")
    private String swaggerTitle;
    @Value("${spring.swagger.contact.name}")
    private String swaggerContactName;
    @Value("${spring.swagger.contact.url}")
    private String swaggerContactUrl;
    @Value("${spring.swagger.contact.email}")
    private String swaggerContactEmail;
    @Value("${spring.swagger.version:@null}")
    private String swaggerVersion;
    @Value("${spring.swagger.desc}")
    private String swaggerDesc;
    private final BuildProperties buildProperties;

    @Bean
    public OpenAPI openApi() {
        String version = swaggerVersion;
        if (StringUtils.isBlank(version)) {
            version = buildProperties.getVersion();
        }
        return new OpenAPI()
                .info(new Info().title(swaggerTitle)
                        .description(swaggerDesc)
                        .version(version)
                        .contact(new Contact().name(swaggerContactName).url(swaggerContactUrl).email(swaggerContactEmail))
                        .license(new License().name("Apache 2.0").url("http://springdoc.org"))
                )
                .externalDocs(new ExternalDocumentation()
                        .description("官方文档")
                        .url("http://springdoc.org"));
    }
}
